-- 运营活动:首充返利


--[[
{
    cmd_id = 1, -- 命令id，用于批量停止、删除活动 
    act_id = 95, -- 活动id (95 首充返利)
    act_name = "首充返利", -- 活动名称
    act_desc = "首充返利而已啦", -- 活动描述
    beg_time = 1493262000,  -- 活动开始时间(unix timestamp)
    end_time = 1693295400,  -- 活动结束时间(unix timestamp)
    config = -- 配置数据
    {
        precent = 30, --百分数 
    },
}
]]

-- 检查配置是否正确，正确则返回 0，否则返回错误码
function oper_act_95_check_cfg(cfg)
	if cfg == nil then return 20 end
	
	local config = cfg.config
	if config == nil then return 21 end
	if config.ItemList == nil then return 23 end
    if type(config.ItemList) ~= "table" then return 24 end

    if not oper_act_check_one_award_cfg(config.ItemList) then
            return 22
        end

	return 0
end

-- 安装活动，成功则返回 0，否则返回错误码
function oper_act_95_setup(cfg)	
	return 0
end

-- 卸载活动
function oper_act_95_unsetup(cfg)	
end

-- 活动开始
function oper_act_95_start(cfg)
    local ssVar = System.getStaticVar()
    ssVar.oper_act_95_start = 1
    ssVar.oper_act_95_cmd = cfg.cmd_id
    ssVar.oper_act_95_act = cfg.act_id

    local data = gRankMgr_GetValueByIndx(0, RANK_DEFINE_OPERATION_95, 1)

    if data then
        local myData = BaseFuc_unserialize(data)
        if myData and myData.cmd_id and myData.cmd_id == cfg.cmd_id and myData.act_id and myData.act_id == cfg.act_id and
            myData.time and cfg.beg_time <= myData.time and  cfg.end_time > myData.time then
            return
        end
    end

    gRankMgr_Clear(RANK_DEFINE_OPERATION_95)
end

-- 活动结束
function oper_act_95_end(cfg)
    local ssVar = System.getStaticVar()
    ssVar.oper_act_95_start = nil
    ssVar.oper_act_95_cmd = nil
    ssVar.oper_act_95_act = nil

    if not cfg then return end
    local config = cfg.config
    if not config then return end
end

-- 打包数据
function oper_act_95_packet_data(sysarg, cfg)

    local ssVar = System.getStaticVar()
    if not ssVar.oper_act_95_start then
        return
    end
    if not cfg then return end
    local config = cfg.config
    if not config then return end

    local actorid = Actor.getActorId(sysarg)
    local data = gRankMgr_GetValueById(actorid, RANK_DEFINE_OPERATION_95, 1)

    local myData = BaseFuc_unserialize(data)

    if not myData then
        myData = {}
    end

    local time = System.getSystemTime()
    local needRefresh = false

    if not myData.myRefreshTime then
        myData.myRefreshTime = time + config.refreshCd
        myData.myRefreshData = {}
        needRefresh = true
    elseif time >= myData.myRefreshTime then
        myData.myRefreshTime = time + config.refreshCd
        myData.myRefreshData = {}
        needRefresh = true
    elseif myData.mySelfTime and not System.isUnixTimeSameDay(myData.mySelfTime, time) then
        myData.myRefreshTime = time + config.refreshCd
        myData.myRefreshData = {}
        needRefresh = true
    end

    if not myData.myRefreshData then
        myData.myRefreshData = {}
        needRefresh = true
    end

    if not myData.myRefreshCount then
        myData.myRefreshCount = 0
    elseif myData.mySelfTime and not System.isUnixTimeSameDay(myData.mySelfTime, time) then
        myData.myRefreshCount = 0
    end

    if needRefresh then
        local choiceItemList = GetItemIdxsRand(config.ItemList, config.drawNum) 
        for idx = 1, #choiceItemList do
            myData.myRefreshData[idx] = {}
            myData.myRefreshData[idx].idx = choiceItemList[idx].itemIdx
            myData.myRefreshData[idx].count = 0
        end

        if not myData.cmd_id then
            myData.cmd_id = cfg.cmd_id
            myData.act_id = cfg.act_id
            myData.time = time
        end

        if not myData.sex then
            myData.sex = Actor.getIntProperty(sysarg, PROP_ACTOR_SEX)
            myData.vocation = Actor.getIntProperty(sysarg, PROP_ACTOR_VOCATION)
        end

        myData.mySelfTime = time

        local newData = BaseFuc_serialize(myData)
        gRankMgr_SetRankEx(actorid, RANK_DEFINE_OPERATION_95, 1, newData)
    end
    

    local pack = oper_act_alloc_init_data_packet(sysarg, cfg.cmd_id, cfg.act_id)
    if (pack == nil) then
        return
    end
    local nextRefreshTime = myData.myRefreshTime - time
    local nextDayRTime = System.getTomorrow() - System.getCurrMiniTime()
    if nextRefreshTime > nextDayRTime then
        nextRefreshTime = nextDayRTime
    end
    DataPack.writeInt(pack, nextRefreshTime)
    DataPack.writeInt(pack, myData.myRefreshCount or 0)
    local len = config.drawNum
    DataPack.writeByte(pack, len)
    for ii = 1, len do
        if not myData.myRefreshData[ii] then
            myData.myRefreshData[ii] = {}
        end
        DataPack.writeShort(pack, myData.myRefreshData[ii].idx or 0)
        DataPack.writeByte(pack, myData.myRefreshData[ii].count or 0)
    end
    DataPack.flush(pack)

end

-- 操作，返回操作结果，0为成功
function oper_act_95_do(sysarg, cfg, clientPkt)
    if cfg == nil then return end
    
    if cfg.state == nil or cfg.state ~= 1 then -- 不在活动中
        Actor.sendTipmsg(sysarg,Lang.ScriptTips.Operation001, ttFlyTip)
        return
    end
    
    local config = cfg.config
    if config == nil then -- 参数错误
        print("error, oper_act_95_do cfg.config confg error")
        return
    end

    local opType = DataPack.readByte(clientPkt)
    local time = System.getSystemTime()
    if opType == 1 then
        local index = DataPack.readShort(clientPkt)
        local actorid = Actor.getActorId(sysarg)
        local data = gRankMgr_GetValueById(actorid, RANK_DEFINE_OPERATION_95, 1)

        local myData = BaseFuc_unserialize(data)

        if not myData then
            myData = {}
        end
        
        local needRefresh = false

        if not myData.myRefreshTime then
            myData.myRefreshTime = time + config.refreshCd
            myData.myRefreshData = {}
            needRefresh = true
        elseif time >= myData.myRefreshTime then
            myData.myRefreshTime = time + config.refreshCd
            myData.myRefreshData = {}
            needRefresh = true
        elseif myData.mySelfTime and not System.isUnixTimeSameDay(myData.mySelfTime, time) then
            myData.myRefreshTime = time + config.refreshCd
            myData.myRefreshData = {}
            needRefresh = true
        end

        if not myData.myRefreshData then
            myData.myRefreshData = {}
            needRefresh = true
        end

        if not myData.myRefreshCount then
            myData.myRefreshCount = 0
        elseif myData.mySelfTime and not System.isUnixTimeSameDay(myData.mySelfTime, time) then
            myData.myRefreshCount = 0
        end

        if needRefresh then
            local choiceItemList = GetItemIdxsRand(config.ItemList, config.drawNum) 
            for idx = 1, #choiceItemList do
                myData.myRefreshData[idx] = {}
                myData.myRefreshData[idx].idx = choiceItemList[idx].itemIdx
                myData.myRefreshData[idx].count = 0
            end

            if not myData.cmd_id then
                myData.cmd_id = cfg.cmd_id
                myData.act_id = cfg.act_id
                myData.time = time
            end

            if not myData.sex then
                myData.sex = Actor.getIntProperty(sysarg, PROP_ACTOR_SEX)
                myData.vocation = Actor.getIntProperty(sysarg, PROP_ACTOR_VOCATION)
            end

            myData.mySelfTime = time

            local newData = BaseFuc_serialize(myData)
            gRankMgr_SetRankEx(actorid, RANK_DEFINE_OPERATION_95, 1, newData)

            oper_act_95_packet_data(sysarg, cfg)
            return
        end
        for idx = 1, config.drawNum do
            if myData.myRefreshData[idx] and myData.myRefreshData[idx].idx == index then
                local item = config.ItemList[index]
                if not item then
                    Actor.sendTipmsg(sysarg,Lang.ScriptTips.Operation015, ttFlyTip)
                    return
                end
                if myData.myRefreshData[idx].count >= item.buyNumLimit then
                    Actor.sendTipmsg(sysarg,Lang.ScriptTips.Operation016, ttFlyTip)
                    return
                end
                if not IsBagGridEnough(sysarg, {item}) then
                    Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0015, ttFlyTip)
                    return
                end

                --[[

                local moneyType = item.priceType
                local moneyNum  = item.price or 0
                if item.discpriceType then              --有折扣价，则按折扣价扣费
                    moneyType   = item.discpriceType
                    moneyNum    = item.discprice or 0
                end
                if moneyNum <= 0 then       --金额错误
                    return
                end

                if not CheckConsumeMoney( sysarg, moneyType, moneyNum, 1, false ) then
                    return
                end

                if not DoConsumeMoney( sysarg, moneyType, moneyNum, 1, LogIds.OperationLogId095, Lang.LogTips.Operation095, false ) then
                    return
                end
                ]]

                if not CheckConsumeCond( sysarg, item.consume, 1 ) then
                    return
                end

                if not DoConsumeCond( sysarg, item.consume, 1, LogIds.OperationLogId095, Lang.LogTips.Operation095 ) then
                    return
                end

                if not myData.myRefreshData[idx].count then
                    myData.myRefreshData[idx].count = 0
                end
                myData.myRefreshData[idx].count = myData.myRefreshData[idx].count + 1

                GiveCommonAward(sysarg, {item}, LogIds.OperationLogId095, Lang.LogTips.Operation095)

                if item.broadcast and item.broadcast ~= 0 then
                    local actorName = Actor.getName(sysarg)
                    local itemStr = Item.getItemLinkMsg(item.id)
                    local str = string.format(Lang.ScriptTips.Operation240, actorName, itemStr)
                    System.broadcastTipmsg(str, ttScreenCenter + ttChatWindow)
                end

                if not myData.cmd_id then
                    myData.cmd_id = cfg.cmd_id
                    myData.act_id = cfg.act_id
                    myData.time = time
                end

                myData.mySelfTime = time

                if not myData.sex then
                    myData.sex = Actor.getIntProperty(sysarg, PROP_ACTOR_SEX)
                    myData.vocation = Actor.getIntProperty(sysarg, PROP_ACTOR_VOCATION)
                end

                local newData = BaseFuc_serialize(myData)
                gRankMgr_SetRankEx(actorid, RANK_DEFINE_OPERATION_95, 1, newData)
                oper_act_95_packet_data(sysarg, cfg)
                return
            end
        end
        Actor.sendTipmsg(sysarg,Lang.ScriptTips.Operation015, ttFlyTip)
    elseif opType == 2 then
        local actorid = Actor.getActorId(sysarg)
        local data = gRankMgr_GetValueById(actorid, RANK_DEFINE_OPERATION_95, 1)

        local myData = BaseFuc_unserialize(data)

        if not myData then
            myData = {}
        end
        
        local needRefresh = false

        if not myData.myRefreshTime then
            myData.myRefreshTime = time + config.refreshCd
            myData.myRefreshData = {}
            needRefresh = true
        elseif time >= myData.myRefreshTime then
            myData.myRefreshTime = time + config.refreshCd
            myData.myRefreshData = {}
            needRefresh = true
        elseif myData.mySelfTime and not System.isUnixTimeSameDay(myData.mySelfTime, time) then
            myData.myRefreshTime = time + config.refreshCd
            myData.myRefreshData = {}
            needRefresh = true
        end

        if not myData.myRefreshCount then
            myData.myRefreshCount = 0
        elseif myData.mySelfTime and not System.isUnixTimeSameDay(myData.mySelfTime, time) then
            myData.myRefreshCount = 0
        end

        if not myData.myRefreshData then
            myData.myRefreshData = {}
            needRefresh = true
        end

        if needRefresh then
            local choiceItemList = GetItemIdxsRand(config.ItemList, config.drawNum) 
            for idx = 1, #choiceItemList do
                myData.myRefreshData[idx] = {}
                myData.myRefreshData[idx].idx = choiceItemList[idx].itemIdx
                myData.myRefreshData[idx].count = 0
            end

            if not myData.cmd_id then
                myData.cmd_id = cfg.cmd_id
                myData.act_id = cfg.act_id
                myData.time = time
            end

            if not myData.sex then
                myData.sex = Actor.getIntProperty(sysarg, PROP_ACTOR_SEX)
                myData.vocation = Actor.getIntProperty(sysarg, PROP_ACTOR_VOCATION)
            end

            myData.mySelfTime = time

            local newData = BaseFuc_serialize(myData)
            gRankMgr_SetRankEx(actorid, RANK_DEFINE_OPERATION_95, 1, newData)

            oper_act_95_packet_data(sysarg, cfg)
        else
            local costNum = myData.myRefreshCount * config.refreshCostAdd + config.firstRefreshCost
            local refreshConsume  = { { type = 10, id = 0, count = costNum,} }        --手工刷新费用
            if not CheckConsumeCond( sysarg, refreshConsume, 1 ) then
                return
            end
            if not DoConsumeCond( sysarg, refreshConsume, 1, LogIds.OperationLogId095, Lang.LogTips.Operation095) then
                return
            end
            myData.myRefreshCount = myData.myRefreshCount + 1
            myData.myRefreshData = {}
            local choiceItemList = GetItemIdxsRand(config.ItemList, config.drawNum) 
            for idx = 1, #choiceItemList do
                myData.myRefreshData[idx] = {}
                myData.myRefreshData[idx].idx = choiceItemList[idx].itemIdx
                myData.myRefreshData[idx].count = 0
            end

            if not myData.cmd_id then
                myData.cmd_id = cfg.cmd_id
                myData.act_id = cfg.act_id
                myData.time = time
            end

            if not myData.sex then
                myData.sex = Actor.getIntProperty(sysarg, PROP_ACTOR_SEX)
                myData.vocation = Actor.getIntProperty(sysarg, PROP_ACTOR_VOCATION)
            end

            myData.mySelfTime = time

            local newData = BaseFuc_serialize(myData)
            gRankMgr_SetRankEx(actorid, RANK_DEFINE_OPERATION_95, 1, newData)

            oper_act_95_packet_data(sysarg, cfg)
        end
    end
end

--[[
-- 跨天
function oper_act_95_new_day_arrive(act_id) 
    
end
]]
